#include "napi/native_api.h"
#include "blake3.h"
#include <unistd.h>
#include "hilog/log.h"
#include "string"

#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200  // 全局domain宏，标识业务领域
#define LOG_TAG "MY_TAG"   // 全局tag宏，标识模块日志tag

using namespace std;
static napi_value getHash(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
    size_t requireArgc = 3;
    size_t argc = 3;
    napi_value args[3] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);
    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);
    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    double value2;
    napi_get_value_double(env, args[2], &value2);
    
    napi_value sum;
    napi_create_double(env, value0 + value1 + value2, &sum);
    
    FILE *file;
    unsigned char buf[65536];
    size_t n;
    blake3_hasher hasher;

    // 打开文件，确保以二进制模式打开
    file = fopen("/data/storage/el2/base/test_harsh.txt", "rb");
    if (!file) {
        OH_LOG_INFO(LOG_APP, "Failed to open file: %s\n", strerror(errno));
    }
    
    // 初始化blake3 hasher
    blake3_hasher_init(&hasher);

    // 循环读取文件内容并更新hasher
    while ((n = fread(buf, 1, sizeof(buf), file)) > 0) {
        blake3_hasher_update(&hasher, buf, n);
    }

    // 检查fread是否因为错误而终止
    if (ferror(file)) {
        OH_LOG_INFO(LOG_APP, "File reading error: %s\n", strerror(errno));
        fclose(file);
        return sum;
    }

    // 关闭文件
    fclose(file);
    uint8_t output[BLAKE3_OUT_LEN];
    blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
    // 如果需要，可以在这里完成哈希计算，例如调用blake3_hasher_finalize
    OH_LOG_INFO(LOG_APP, "!!!!!!!!!!!!!!!!!!!!!!!!!!!" );

    string hashValue="";
      for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
        hashValue+=  std::to_string(output[i]);
      }
    hashValue="哈希值 "+hashValue;
    OH_LOG_INFO(LOG_APP, "!!!!!!!!!!!!!%{public}s", hashValue.c_str());
    
    napi_value result;

    char strBuf[256];
    napi_create_string_utf8(env,  hashValue.c_str(), sizeof(hashValue), &result);
    // Copies UTF-16 encoded bytes from a string into a buffer.
// NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env,
//                                                 const char* str,
//                                                 size_t length,
//                                                 napi_value* result);
    return result;

}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "getHash", nullptr, getHash, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
